package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.QueryVo;
import com.itheima.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements UserDao {

    private SqlSessionFactory sqlSessionFactory;


    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public List<User> findAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("com.itheima.dao.UserDao.findAll");
        sqlSession.close();
        return userList;
    }

    @Override
    public void userSave(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int insert = sqlSession.insert("com.itheima.dao.UserDao.userSave", user);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("UserDao ==> userSave : " + insert);
    }

    @Override
    public void userUpdate(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int update = sqlSession.update("com.itheima.dao.UserDao.userUpdate", user);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("UserDao => : " + update);
    }

    @Override
    public void userDeleteById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int i = sqlSession.delete("com.itheima.dao.UserDao.userDeleteById", id);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("UserDao => userDeleteById : " + i);
    }

    @Override
    public User findById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User o = sqlSession.selectOne("com.itheima.dao.UserDao.findById", id);
        sqlSession.commit();
        sqlSession.close();
        return o;
    }

    @Override
    public List<User> findByName(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("com.itheima.dao.UserDao.findByName", username);
        sqlSession.commit();
        sqlSession.close();
        return userList;
    }

    @Override
    public int findTotal() {
        return 0;
    }

    @Override
    public int insertGetId(User user) {
        return 0;
    }

    @Override
    public List<User> findVoByName(QueryVo queryVo) {
        return null;
    }

    @Override
    public List<User> findAll2() {
        return null;
    }

    @Override
    public List<User> findByCondition(User user) {
        return null;
    }

    @Override
    public List<User> findByIds(QueryVo vo) {
        return null;
    }
}
